<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AdminMenu extends Model
{
    protected $table = "admin_menu";
    protected $appends = ["prev_menu"];

    public function getPrevMenuAttribute(){
        if ($this->attributes['prev_id']==0){
            return $this->attributes['prev_menu'] = "一级菜单";
        }else{
            $menuData = $this->where("id",$this->attributes['prev_id'])->first();
            return $this->attributes['prev_menu'] = $menuData->menu_name;
        }
    }


    public static function searchMenu($id,$resource){
        foreach ($resource as $item){
            if ($id == $item->id){
                return 1;
            }else {
                foreach ($item->child as $value) {
                    if ($id == $value->id) {
                        return 1;
                    } else {
                        continue;
                    }
                }
            }
        }
        return 0;
    }
}
